package cn.hiapi.user.api;

import cn.hiapi.basic.ResponseEntity;
import cn.hiapi.basic.controller.BasicQueryController;
import cn.hiapi.basic.entity.BasicEntity;
import cn.hiapi.basic.security.TokenGet;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.publics.vo.ShopBaseVo;
import cn.hiapi.shop.entity.shop.Shop;
import cn.hiapi.shop.service.shop.ShopService;
import cn.hiapi.shopkeeper.entity.activity.Activity;
import cn.hiapi.shopkeeper.entity.user.HistoryRecord;
import cn.hiapi.shopkeeper.service.activity.*;
import cn.hiapi.shopkeeper.service.user.HistoryRecordService;
import cn.hiapi.user.query.HistoryRecordQuery;
import cn.hiapi.user.vo.HistoryRecordVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author AdinZ
 * @date 2022/12/31 22:01
 */
@RestController
@RequestMapping("/user/history-record")
public class HistoryRecordController extends BasicQueryController<HistoryRecord, Long, HistoryRecordVo, HistoryRecordQuery> {
    private final HistoryRecordService basicService;

    protected HistoryRecordController(HistoryRecordService basicService, DispatchContext dispatchContext) {
        super(basicService, dispatchContext);
        this.basicService = basicService;
    }


    @Override
    public HistoryRecordQuery setQuery(HistoryRecordQuery query) {
        query.setUid(TokenGet.get().getFid());
        query.setDirection(Sort.Direction.DESC);
        query.setProperties("id");
        return super.setQuery(query);
    }

    @Override
    public List<HistoryRecordVo> toListVo(List<HistoryRecord> list, String[] fields) {
        return super.toList(list, fields).stream().filter(e -> e != null).collect(Collectors.toList());
    }

    @Override
    public HistoryRecordVo toData(HistoryRecord data, String[] fields) {
        Activity e = this.dispatchContext.getServiceOne(ActivityService.class).get(data.getAid());
        if (e != null) {
            HistoryRecordVo vo = e.copy(new HistoryRecordVo());
            vo.setHistoryId(data.getId());

            Shop shop = this.dispatchContext.getServiceOne(ShopService.class).get(e.getShopId());
            if (shop != null)
                vo.setShop(shop.copy(new ShopBaseVo()));

            BasicEntity entity = this.dispatchContext.getServiceOne(ActivityFactory.class).get(e.getType()).getByAid(e.getId());
            if(entity!=null) {
                JSONObject expand = JSON.parseObject(JSON.toJSONString(entity));
                vo.setExpand(expand);
            }

            return vo;
        } else return null;
    }


    @ApiOperation(value = "删除全部足迹", notes = "删除全部足迹", nickname = "AdinZ")
    @PostMapping("/delete-all")
    public ResponseEntity deleteAll() {
        this.basicService.deleteAll(TokenGet.get().getFid());
        return toSuccess();
    }


    @ApiOperation(value = "删除指定", notes = "删除指定", nickname = "AdinZ")
    @PostMapping("/delete")
    public ResponseEntity delete(@RequestParam("id") Long[] id) {
        this.basicService.deleteByUid(TokenGet.get().getFid(), id);
        return toSuccess();
    }


}
